import Vue from 'vue'
import axios from 'axios'
import authorityService from '../service/AuthorityService.js';
import {eventHub} from '../service/EventHub';


//网络配置
axios.defaults.withCredent = true
axios.interceptors.request.use(
  config => {
    config.headers.common.isJson = 'true';
    config.headers.common.code = authorityService.getCode("code");
    return config;
  },
  error => {
    return Promise.reject(error.response);
  });

axios.interceptors.response.use((res) => {
  let code = res.data.code;
  if (code === 200) {
    return res.data.data;
  }

  if (code === 401) {
    authorityService.toLoginPage()
    return Promise.reject(res.data)
  }

  if (code === 403) {
    window.error("权限不足，请联系管理员。处理完后请重新登录！");
    return Promise.reject(res.data)
  }

  if (code === 500) {
    window.error("系统处理异常" + (res.data.msg ? (":" + res.data.msg) : "") + "!");
    return Promise.reject(res.data)
  }

}, function (error) {
  eventHub.$emit("404", {});
})

Vue.prototype.$http = axios
export default axios;
